package com.masterclient.app.businessmodule.minemodule.index.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.airbnb.lottie.LottieAnimationView;
import com.airbnb.lottie.LottieComposition;
import com.airbnb.lottie.LottieDrawable;
import com.masterclient.app.R;

public class PersonTopItemView extends FrameLayout {

    private LottieAnimationView lottieAnimationView;
    private ImageView imageView;
    private TextView titleView;
    private TextView numView;

    public PersonTopItemView(@NonNull Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.item_person_top,this);
        initUI();
    }

    public PersonTopItemView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.item_person_top,this);
        initUI();
    }

    private void initUI() {
        lottieAnimationView = findViewById(R.id.pt_animation_view);
        imageView = findViewById(R.id.pt_image_view);
        titleView = findViewById(R.id.pt_text_view);
        numView = findViewById(R.id.pt_num_view);
    }

    public void showData(String title,String json,int num) {
        if (title.equals("已签到")) {
            imageView.setImageResource(R.mipmap.icon_person_sign);
            imageView.setVisibility(VISIBLE);
            lottieAnimationView.setVisibility(GONE);
        } else {
            imageView.setVisibility(GONE);
            lottieAnimationView.setVisibility(VISIBLE);
            playShopAnimation(json);
        }
        titleView.setText(title);
        if (num !=-1) {
            numView.setText("("+num+")");
            numView.setVisibility(VISIBLE);
        } else {
            numView.setVisibility(GONE);
        }
    }

    //播放大礼物动画
    public void playShopAnimation(String json) {
        LottieComposition composition = LottieComposition.Factory.fromFileSync(getContext(),json);
        lottieAnimationView.cancelAnimation();
        lottieAnimationView.setProgress(0);
        lottieAnimationView.setRepeatCount(-1);
        lottieAnimationView.setRepeatMode(LottieDrawable.RESTART);
        lottieAnimationView.setComposition(composition);
        lottieAnimationView.playAnimation();
    }

}
